package com.hgyc.mom.qms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgyc.mom.qms.entity.QmsIqcRecord;
import com.hgyc.mom.qms.mapper.QmsIqcRecordMapper;
import com.hgyc.mom.qms.service.QmsIqcRecordService;
import com.hgyc.mom.qms.vo.QmsIqcDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 来料检验单缺陷记录Service业务层实现
 *
 * @author fwj
 * @since 2025-09-27
 */
@Slf4j
@Service
public class QmsIqcRecordServiceImpl extends ServiceImpl<QmsIqcRecordMapper, QmsIqcRecord> implements QmsIqcRecordService {

    @Override
    public List<QmsIqcRecord> getByParentId(Long parentId) {
        LambdaQueryWrapper<QmsIqcRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QmsIqcRecord::getDetailId, parentId);
        return list(wrapper);
    }

    @Override
    public void deleteByParentId(Long parentId) {
        LambdaQueryWrapper<QmsIqcRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QmsIqcRecord::getDetailId, parentId);
        remove(wrapper);
    }

    @Override
    public void deleteBatchByParentIds(List<Long> parentIds) {
        LambdaQueryWrapper<QmsIqcRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(QmsIqcRecord::getDetailId, parentIds);
        remove(wrapper);
    }

    @Override
    public void saveRrecord(QmsIqcDetailVO qmsIqcDetailVO) {
        //删除来料检验项明细
        if (!CollectionUtils.isEmpty(qmsIqcDetailVO.getDeleteIds())) {
            removeBatchByIds(qmsIqcDetailVO.getDeleteIds());
        }

        //新增来料检验项明细
        List<QmsIqcRecord> addRecords = qmsIqcDetailVO.getAddRecords();
        if (!CollectionUtils.isEmpty(addRecords)) {
            addRecords.forEach(item -> {
                item.setId(null);
            });
            saveBatch(addRecords);
        }

        //更新来料检验项明细
        if (!CollectionUtils.isEmpty(qmsIqcDetailVO.getUpdateRecords())) {
            List<QmsIqcRecord> updateIqcDetails = qmsIqcDetailVO.getUpdateRecords();
            updateBatchById(updateIqcDetails);
        }
    }
}
